//
//  FYFileHandle.m
//  AVPlayer 缓存测试
//
//  Created by 寿煜宇 on 16/9/2.
//  Copyright © 2016年 Fyus. All rights reserved.
//

#import "FYFileHandle.h"
@interface FYFileHandle ()
//读写 文件  操作
@property (nonatomic, strong) NSFileHandle * writeFileHandle;
@property (nonatomic, strong) NSFileHandle * readFileHandle;

@end
@implementation FYFileHandle
//创建临时文件
+ (BOOL)createTempFile {
    NSFileManager * manager = [NSFileManager defaultManager];
    NSString * path = [[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"] stringByAppendingPathComponent:@"MusicTemp.mp3"];
    
    if ([manager fileExistsAtPath:path]) {
        [manager removeItemAtPath:path error:nil];
    }
    return [manager createFileAtPath:path contents:nil attributes:nil];
}
//读取临时文件
+ (void)writeTempFileData:(NSData *)data {
    NSFileHandle * handle = [NSFileHandle fileHandleForWritingAtPath:[[NSHomeDirectory( ) stringByAppendingPathComponent:@"tmp"] stringByAppendingPathComponent:@"MusicTemp.mp3"]];
    [handle seekToEndOfFile];
    [handle writeData:data];
}

+ (NSData *)readTempFileDataWithOffset:(NSUInteger)offset length:(NSUInteger)length {
    NSFileHandle * handle = [NSFileHandle fileHandleForReadingAtPath:[[NSHomeDirectory( ) stringByAppendingPathComponent:@"tmp"] stringByAppendingPathComponent:@"MusicTemp.mp3"]];
    [handle seekToFileOffset:offset];
    return [handle readDataOfLength:length];
}
//保存临时文件到缓存文件
+ (void)cacheTempFileWithFileName:(NSString *)name {
    NSFileManager * manager = [NSFileManager defaultManager];
    NSString * cacheFolderPath = [[NSHomeDirectory( ) stringByAppendingPathComponent:@"Library"] stringByAppendingPathComponent:@"MusicCaches"];
    if (![manager fileExistsAtPath:cacheFolderPath]) {
        [manager createDirectoryAtPath:cacheFolderPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString * cacheFilePath = [NSString stringWithFormat:@"%@/%@", cacheFolderPath, name];
    BOOL success = [[NSFileManager defaultManager] copyItemAtPath:[[NSHomeDirectory( ) stringByAppendingPathComponent:@"tmp"] stringByAppendingPathComponent:@"MusicTemp.mp3"] toPath:cacheFilePath error:nil];
    
    NSLog(@"cache file : %@", success ? @"success" : @"fail");
}
//缓存文件是否存在
+ (NSString *)cacheFileExistsWithURL:(NSURL *)url {
    NSString * cacheFilePath = [NSString stringWithFormat:@"%@/%@", [[NSHomeDirectory( ) stringByAppendingPathComponent:@"Library"] stringByAppendingPathComponent:@"MusicCaches"], [[url.path componentsSeparatedByString:@"/"] lastObject]];
    
    NSLog(@"输出地址：%@", cacheFilePath);

    if ([[NSFileManager defaultManager] fileExistsAtPath:cacheFilePath]) {
        return cacheFilePath;
    }
       return nil;
}
//清空缓存文件
+ (BOOL)clearCache {
    NSFileManager * manager = [NSFileManager defaultManager];
    return [manager removeItemAtPath:[[NSHomeDirectory( ) stringByAppendingPathComponent:@"Library"] stringByAppendingPathComponent:@"MusicCaches"] error:nil];
}


+ (NSString *)axcFileSizeWithToString:(unsigned long long)fileSize

{

    NSInteger KB = 1024;

    NSInteger MB = KB*KB;

    NSInteger GB = MB*KB;

    

    if (fileSize < 10){

        return @"0 B";

    }else if (fileSize < KB){

        return @"< 1 KB";

    }else if (fileSize < MB){

        return [NSString stringWithFormat:@"%.1f KB",((CGFloat)fileSize)/KB];

    }else if (fileSize < GB){

        return [NSString stringWithFormat:@"%.1f MB",((CGFloat)fileSize)/MB];

    }else{

        return [NSString stringWithFormat:@"%.1f GB",((CGFloat)fileSize)/GB];

    }

}

+(NSInteger)getSizeOfFilePath:(NSString *)filePath{
    /** 定义记录大小 */
    NSInteger totalSize = 0;
    /** 创建一个文件管理对象 */
    NSFileManager * manager = [NSFileManager defaultManager];
    /**获取文件下的所有路径包括子路径 */
    NSArray * subPaths = [manager subpathsAtPath:filePath];
    /** 遍历获取文件名称 */
    for (NSString * fileName in subPaths) {
        /** 拼接获取完整路径 */
        NSString * subPath = [filePath stringByAppendingPathComponent:fileName];
        /** 判断是否是隐藏文件 */
        if ([fileName hasPrefix:@".DS"]) {
            continue;
        }
        /** 判断是否是文件夹 */
        BOOL isDirectory;
        [manager fileExistsAtPath:subPath isDirectory:&isDirectory];
        if (isDirectory) {
            continue;
        }
        /** 获取文件属性 */
        NSDictionary *dict = [manager attributesOfItemAtPath:subPath error:nil];
        /** 累加 */
        totalSize += [dict fileSize];
        
    }
    /** 返回 */
    return totalSize;
}
@end
